// pages/login/login.js
const util = require("../../utils/util.js");

var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        codes: 86,
        disable: false,
        num: 60,
        codesTrue: true,
        isChinese: wx.getStorageSync("isChinese")
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // let that = this;
        let data = {};
        this.setData({
            isChinese: wx.getStorageSync("isChinese")
        })
        wx.setNavigationBarTitle({
            title: this.data.isChinese ? "发送验证码" : "Get verification code"
        })
    },
    onShow() {
        if (wx.getStorageSync("areaCode")) {
            this.setData({
                codes: wx.getStorageSync("areaCode"),
                countryName: wx.getStorageSync("countryNames")
            });
        }
    },
    codes() {
        wx.navigateTo({
            url: "../areaCode/areaCode"
        });
    },
    changePhone(e) {
        //获取号码
        let str = e.detail.value;
        this.setData({
            phoneNum: str
        });
    },
    getCode: function() {
        //发送验证码
        if (this.data.disable) return;
        let phone = this.data.phoneNum,
            _this = this,
            code = "",
            flag = /^1[34578]\d{9}$/.test(phone);
        if (!flag && this.data.codes == '86') {
            wx.showModal({
                title: _this.data.isChinese ? "提示" : "Prompt",
                content: _this.data.isChinese ?
                    "请输入正确格式的手机号" : "Please enter the correct phone number",
                showCancel: false
            });
            return;
        }
        this.setData({
            disable: true,
            regCode: code
        });
        let timer = setInterval(function() {
            let num = _this.data.num;
            _this.setData({
                num: --num
            });
            console.log(num)
            if (_this.data.num == 1) {
                clearInterval(timer);
                _this.setData({
                    num: 60,
                    disable: false
                });
            }
        }, 1000);
        app.getApi.getProducts("POST", {
            userId: app.globalData.uuid,
            object: "+" + this.data.codes + this.data.phoneNum,
            signName: "OnePartyGo",
            verificationType: "SHORT_MOBILE",
            method: "xntalk.vefificationCode.create"
        }).then(res => {
            if (res.data.errors.length == 0) {
                _this.setData({
                    codesTrue: false
                })
                wx.showModal({
                    title: _this.data.isChinese ? '提示' : 'Prompt',
                    content: _this.data.isChinese ? '请输入您收到的验证码, 发送手机号为:+' + this.data.codes + this.data.phoneNum : 'Enter the code sent to:+' + this.data.codes + this.data.phoneNum,
                    showCancel: false
                })
                return;
            } else {
                console.log(res)
                wx.showModal({
                    title: _this.data.isChinese ? '提示' : 'Prompt',
                    content: res.data.firstErrorMessage,
                    showCancel: false
                })

                // return;
            }
        }).catch(res => {
            wx.showModal({
                title: _this.data.isChinese ? '提示' : 'Prompt',
                content: _this.data.isChinese ? '验证码发送失败,请重新尝试' : 'Unable to send the verification code. Please try again later.',
                showCancel: false
            })
            return;
        })
    },
    bindsubmit(e) {
        var _this = this;
        if (e.detail.value.code == "") {
            wx.showModal({
                title: _this.data.isChinese ? "提示" : "Prompt",
                content: _this.data.isChinese ?
                    "请输入验证码" : "Enter the verification code",
                showCancel: false
            });
            return;
        }
        app.getApi.getProducts(
            "POST", {
                code: e.detail.value.code,
                object: "+" + this.data.codes + this.data.phoneNum,
                type: "SHORT_MOBILE",
                method: "xntalk.vefificationCode.check",
                deleteAfterCheck: 1
            }).then(res => {
            if (res.data.isVerified) {
                // wx.showModal({
                //     title: _this.data.isChinese ? "提示" : "Prompt",
                //     content: _this.data.isChinese ?
                //         "请输入您收到的验证码, 发送手机号为:+" + this.data.codes + this.data.phoneNum : "Enter the code sent to:+" +
                //         this.data.codes +
                //         this.data.phoneNum,
                //     showCancel: false
                // });
                app.globalData.mobilePhone = _this.data.codes + _this.data.phoneNum,
                    wx.navigateBack({
                        delta: 1
                    })
                return;
            } else {
                wx.showModal({
                    title: _this.data.isChinese ? "提示" : "Prompt",
                    content: _this.data.isChinese ? "验证码错误" : "Verification code error",
                    showCancel: false
                });
                return;
            }
        })
    }
});